package com.qfedu.dao.impl;

import com.qfedu.bean.User;
import com.qfedu.dao.UserDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class UserDaoImpl implements UserDao {


    @Override
    public List<User> selectUserList() throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> selectUserList = sqlSession.selectList("test.selectUserById");
        System.out.println(selectUserList);
        sqlSession.close();
        return selectUserList;
    }

    @Override
    public User selectUserById(Integer id) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = sqlSession.selectOne("test.selectUserById", id);
        System.out.println(user);
        sqlSession.close();
        return user;
    }

    @Override
    public List<User> selectUserListLikeName1(String username) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> selectUserList = sqlSession.selectList("test.selectUserListLikeName1",username);
        System.out.println(selectUserList);
        sqlSession.close();
        return selectUserList;

    }

    @Override
    public List<User> selectUserListLikeName2(String username) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> selectUserList = sqlSession.selectList("test.selectUserListLikeName2",username);
        System.out.println(selectUserList);
        sqlSession.close();
        return selectUserList;
    }

    @Override
    public List<User> selectUserListLikeName3(String username) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> selectUserList = sqlSession.selectList("test.selectUserListLikeName3",username);
        System.out.println(selectUserList);
        sqlSession.close();
        return selectUserList;
    }

    @Override
    public List<User> selectUserListLikeName4(String username) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> selectUserList = sqlSession.selectList("test.selectUserListLikeName4",username);
        System.out.println(selectUserList);
        sqlSession.close();
        return selectUserList;
    }

    @Override
    public void addUser(User inputUser) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        int insert = sqlSession.insert("test2.addUser", inputUser);
        System.out.println(insert);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void addUser2(User inputUser) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        int insert = sqlSession.insert("test2.addUser2", inputUser);
        System.out.println(insert);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deletdUserById(Integer id) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        int delete = sqlSession.delete("test2.deletdUserById", id);
        System.out.println(delete);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void updateUser(User inputUser) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        int update = sqlSession.update("test2.updateUser", inputUser);
        System.out.println(update);
        sqlSession.commit();
        sqlSession.close();
    }
}
